//
// Created by Administrator on 2021/8/19.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    string reverseVowels(string s) {
        // 1、栈+保留元音字母位置
        // 2、找到首尾的第i个元音字母，交换  双指针
        string vowel = "aeiouAEIOU";
        int left = 0, right = (int) s.size();
        while (left < right) {
            if (vowel.find(s[left]) == string::npos) {
                ++left;
                continue;
            }
            if (vowel.find(s[right]) == string::npos) {
                --right;
                continue;
            }
            swap(s[left], s[right]);
            ++left;
            --right;
        }
        return s;
    }
};
class Solution2 { // 题解
public:
    string reverseVowels(string s) {
        auto isVowel = [vowels = "aeiouAEIOU"s](char ch) {
            return vowels.find(ch) != string::npos;
        };

        int n = s.size();
        int i = 0, j = n - 1;
        while (i < j) {
            while (i < n && !isVowel(s[i])) {
                ++i;
            }
            while (j > 0 && !isVowel(s[j])) {
                --j;
            }
            if (i < j) {
                swap(s[i], s[j]);
                ++i;
                --j;
            }
        }
        return s;
    }
};

int main() {
    Solution solution;
    cout << solution.reverseVowels("leetcode") << endl;
    cout << solution.reverseVowels("hello") << endl;
    return 0;
}
